<?php

	eb_include_module_file("Core/UrlRouteVO");

	class CoreFilter implements IFilter
	{		
		var $routes;
		
		public function init($data)
		{
			$this->routes = EbModel::getModel("Core","UrlRoute")->selectPrepared();
		}
	
		public function doFilter($request,$response)
		{		
			if (is_null($response))
			{
				$session = EbController::$session;
		
				// 1. Create the user
				if (isset($session->roles))
				{
					Community_BasicUser::addRoles(split(',',$session->roles));
				}
				else
				{
					Community_BasicUser::addRoles(1);
				}				
			
						
				// 2. Get the route
				$urlRoutingManager = new UrlRoutingManager($this->routes);
				
				// Administration operation?
				if ($request->queryuri === '_')
				{
					$request->event = new Event('Core/AdminOperation',$request->data);
					return ;
				}
								
				if (($matchedRoute = $urlRoutingManager->match($request->queryuri)) === false)
				{
					eb_redirect('/404');
				}
				else
				{						
					// Get the meta data of the matched route
					//EbModel::getModel('Core','ObjectMetaData')->selectByObject($matchedRoute->ob_id);
					
					foreach ($matchedRoute->data AS $key=>$value)
					{
						$request->setData($key,$value);
					}
					
					$request->setData('__ro_id',$matchedRoute->ur_id);
														
					if ($matchedRoute->type == UrlRouteVO::VIEW)
					{
						$request->event = new Event("Core/UrlRoutingToPage",$matchedRoute);
					}
					else if ($matchedRoute->type == UrlRouteVO::MODEL)
					{
						$request->event = new Event("Core/UrlRoutingToModel",$matchedRoute->data);
					}
					else
					{
						$request->event = new Event($matchedRoute->data["module"]."/".$matchedRoute->data["file"],$request->data);
					}
				}
				
				if (Community_BasicUser::hasRole(2))
				{
					EbView::includeEbuildyCss('eb_misc');
					EbView::includeJQueryPlugin();
					EbView::includeEbuildyJavascript('eb-live-editor');
				}
			}
			else
			{
				if (Community_BasicUser::hasRole(2))
				{					
					ob_start();
					
					include (EBUILDY_PATH.'/template/DebugTemplate.php');
					
					$debug = ob_get_clean();
													
					$response->content = str_ireplace('</body>',$debug.'</body>',$response->content);
				}
			}
		}
	
	}
	
	class UrlRoutingManager
	{
		var $routes;
		
		public function __construct($routes)
		{
			$this->routes = $routes;
		}
		
		public function match($url)
		{	
			foreach ($this->routes AS $route)
			{
				if (strpos($route->url,'<') === false)
				{
					if ($this->match_static_route($url,$route->url))
					{
						return $route;
					}
				}
			}
			
			foreach ($this->routes AS $route)
			{
				if (strpos($route->url,'<') !== false)
				{
					if (($r = $this->match_dynamic_route($url,$route->url)) !== false)
					{
						if (is_null($route->data))
						{
							$route->data = $r;
						}
						else
						{
							$route->data = array_merge(eb_parse_parameters_string($route->data),$r);
						}
											
						return $route;
					}
				}
			}
			
			return false;
		}
		
		function match_static_route($url,$route)
		{
			return $url == $route;
		}
	
		function match_dynamic_route($url,$route)
		{
			$result = false;
			
			$route = str_replace("/","\/",$route);
			$route = str_replace(".","\.",$route);
			$route = str_replace("<","(?P<",$route);
			$route = str_replace(">",">[^\/]*)",$route);

			if (preg_match("/^$route$/",$url,$matches) > 0)
			{
				$result = array();
								
				foreach ($matches AS $key=>$value)
				{
					if (!is_numeric($key))
					{
						$result[$key] = $value;
					}
				}
			}
			
			return $result;
		}		
	}